package controllers;

import java.io.IOException;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.IpAddressModel;

import data.IpAddressData;

public class CheckIpServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		TreeMap check = new TreeMap();
		String ip = (String) request.getParameter("ip");
		
		if (ip != null && ip.isEmpty()) {
			response.sendRedirect("/message?txt=page-not-found");
			return;
		}
		
		IpAddressData ipData = new IpAddressData();
		IpAddressModel ipAddress = null;
		if (ip != null) {
			ipAddress = ipData.get(ip);
		} else {
			ip = "";
		}
		
		request.setAttribute("ipAddress", ipAddress);
		request.setAttribute("ip", ip);
		request.setAttribute("check", check);
		try {
			request.getRequestDispatcher("/pages/checkip_info.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
}
